<?php

class CronjobController extends GxController {
    /* Weekly class batch schedule */

    public function actionWeeklybatch() {
        $batchData = ClassBatch::model()->findAll('schedule_id=:schedule_id', array(':schedule_id' => REPEAT_CLASS_WEEKLY));
        foreach ($batchData as $batchData) {
            $model = new Classbatchweelymonthly;
            $nextWeekInt = strtotime('+1 week', strtotime($batchData->start_date));
            $nextWeek_start_date = date('Y-m-d', $nextWeekInt);
            $checkdata = $model->findByAttributes(array('classes_id' => $batchData->classes_id, 'class_batch_id' => $batchData->id));

            if ($checkdata <> "" && $checkdata->weekly_monthly_start_date < CURRENT_DATE) {
                $nextWeekInt = strtotime('+1 week', strtotime($checkdata->weekly_monthly_start_date));
                $nextWeek_start_date = date('Y-m-d', $nextWeekInt);
                $model->updateByPk($checkdata->id, array('weekly_monthly_start_date' => $nextWeek_start_date));
            } elseif ($checkdata == "" && $batchData->start_date < CURRENT_DATE) {
                $model->setattribute('classes_id', $batchData->classes_id);
                $model->setattribute('class_batch_id', $batchData->id);
                $model->setattribute('weekly_monthly_start_date', $nextWeek_start_date);
                $model->save();
            }
        }
    }

    /* Monthly class batch schedule */

    public function actionMonthlybatch() {
        $batchData = ClassBatch::model()->findAll('schedule_id=:schedule_id', array(':schedule_id' => REPEAT_CLASS_MONTHLY));
        foreach ($batchData as $batchData) {           
            $model = new Classbatchweelymonthly;
            $nextMonth_start_date = addOneMonthinBatch($batchData->start_date);          
            $checkdata = $model->findByAttributes(array('classes_id' => $batchData->classes_id, 'class_batch_id' => $batchData->id));
            if ($checkdata <> "" && $checkdata->weekly_monthly_start_date < CURRENT_DATE) {
                $nextMonth_start_date = addOneMonthinBatch($checkdata->weekly_monthly_start_date);
                $model->updateByPk($checkdata->id, array('weekly_monthly_start_date' => $nextMonth_start_date));
            } elseif ($checkdata == "" && $batchData->start_date < CURRENT_DATE) {
                $model->setattribute('classes_id', $batchData->classes_id);
                $model->setattribute('class_batch_id', $batchData->id);
                $model->setattribute('weekly_monthly_start_date', $nextMonth_start_date);
                $model->save();
            }
        }
    }

}